package com.example.mybank.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybank.entity.*;
import com.example.mybank.mapper.DebitMapper;
import com.example.mybank.mapper.EngineMapper;
import com.example.mybank.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class EngineService {

//    @Autowired
    engine engineDefault;

    // 毫秒到天转换
    private static Long millToDays = 86400000L;

    @Autowired
    EngineMapper engineMapper;

    @Autowired
    DebitMapper debitMapper;

    @Autowired
    UsersMapper usersMapper;



    public boolean isPass(Users users, Product product){

        if(true)return true;

        QueryWrapper<engine> findRiskEngine = new QueryWrapper<>();


        findRiskEngine.eq("id", product.getEngine());
        engine riskEngine = engineMapper.selectOne(findRiskEngine);

        // 决策引擎是否启用
        if(riskEngine.getIs_use() == false) {
            return true;
        }

        // 获取当前用户
//        QueryWrapper<Users> findUser = new QueryWrapper<>();
//        findUser.eq("id", account.getUsers());
//        Users users = usersMapper.selectOne(findUser);

        // 判断工作需求
        if(riskEngine.getWork_need() == true) {
            if(users.getHas_work() == false) return false;
        }

        // 判断失信执行人
        if(riskEngine.getHonest_need() == true) {
            if(users.getHas_credit() == false) return false;
        }

        // 判断年龄
        if(riskEngine.getAge_need() == true) {
            if(users.getAge() < riskEngine.getAge_min()) return false;
        }

        // 判断贷款逾期
        if(riskEngine.getDelay_need() == true) {
            Calendar calendar = Calendar.getInstance();
            Date nowTime = calendar.getTime();
            calendar.add(Calendar.YEAR, (int) (-1 * riskEngine.getDelay_year()));
            Date earlyAge = calendar.getTime();
            QueryWrapper<Debit> QueryDebit = new QueryWrapper<>();

            // 逾期未还筛选
            QueryDebit
                    .and(i->i.eq("is_delay", true).gt("date_to", earlyAge));

            List<Debit> debits = debitMapper.selectList(QueryDebit);

            Long money_sum = 0L;

            // 判断是否有超出最大逾期金额的
            for(int i = 0;i < debits.size();i++){
                Debit debit = debits.get(i);
                if(debit.getMoney() > riskEngine.getDelay_money_max()) return false;
                if(debit.getIs_return() == false) {
                    if((nowTime.getTime() - debit.getDate_to().getTime()) / millToDays < riskEngine.delay_days_max) {
                        debits.remove(i);
                        i--;
                    }
                    else {
                        money_sum += debit.getMoney();
                    }
                }
                else {
                    if((debit.getDate_return().getTime() - debit.getDate_to().getTime()) / millToDays < riskEngine.delay_days_max) {
                        debits.remove(i);
                        i--;
                    }
                    else {
                        money_sum += debit.getMoney();
                    }
                }
            }

            // 最大逾期数超限
            if(debits.size() > riskEngine.getDelay_count_max()) return false;

            // 逾期总金额超限
            if(money_sum > riskEngine.getDelay_money_sum()) return false;
        }

        return true;
    }
}
